package com.example.huitong.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.huitong.R;
import com.example.huitong.ui.LogisticDetailsActivity;
import com.example.huitong.util.Constant;

public class QueryFragment extends Fragment implements OnClickListener, OnItemSelectedListener{
	
	private Spinner mSpn;
	private EditText mEtNum;
	private View mBtnQuery;
	private String[] mExpressIds;
	private String[] mExpressNames;
	private int mSelection = -1;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_query, container, false);
		initData();
		initViews(rootView);
		return rootView;
	}
	
	private void initData() {
		// TODO Auto-generated method stub
		mExpressIds = getActivity().getResources().getStringArray(R.array.express_id);
		mExpressNames = getActivity().getResources().getStringArray(R.array.express_name);
	}

	private void initViews(View view) {
		// TODO Auto-generated method stub
		getActivity().getActionBar().setDisplayShowHomeEnabled(true);
		
		mSpn = (Spinner) view.findViewById(R.id.spinner1);
		mEtNum = (EditText) view.findViewById(R.id.editText1);
		mBtnQuery = view.findViewById(R.id.layout_query);
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.express_name, android.R.layout.simple_spinner_item);  
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		mSpn.setAdapter(adapter);
		mSpn.setOnItemSelectedListener(this);
		
		mEtNum.setText("768685345166");
		mSpn.setSelection(1);
		
		mBtnQuery.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String num = mEtNum.getText().toString().trim();
		if(mSelection == -1){
			return;
		}
		Intent intent = new Intent(getActivity(), LogisticDetailsActivity.class);
		intent.putExtra(Constant.KEY_EXP_ID, mExpressIds[mSelection]);
		intent.putExtra(Constant.KEY_EXP_NAME, mExpressNames[mSelection]);
		intent.putExtra(Constant.KEY_EXP_NUM, num);
		getActivity().startActivity(intent);
		
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		mSelection = position;
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub
		
	}
}
/*
 * 

EMS快递	ems
申通快递	shentong
圆通快递	yuantong
中通快递	zhongtong
天天快递	tiantian
韵达快递	yunda
顺丰快递	shunfeng
宅急送快递	zhaijisong
中国邮政平邮	pingyou
速尔快递	sure
快捷快递	kuaijie
中铁快运	zhongtie

*/